<template>
  <div>
    <el-row>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
        <template #allNum>
          <div class="tabelAomut">
            <div>
              精品总金额：<span>￥{{ jpSum }}</span>
            </div>
            <div>
              精品优惠金额：<span>￥{{ jpSum-discountsAmount }}</span>
            </div>
            <div>
              精品折后总金额：<span>{{ discountsAmount }}</span>
            </div>
          </div>
        </template>
      </data-table>
    </el-row>
    <el-row>
      <el-col align="end">
        <el-button
          type="success"
          @click="confimReturn"
        >确认退料</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { rapidPartsPage } from '@/api/after-sale/workOrderManage/fast' //快速单子api
import baseMixin from '@/mixins/base'

export default {
  name: 'ReturnBoutique',
  components: {},
  props: ['workOrderId', 'workOrderType'], // workOrderType 工单类型 1快速单
  mixins: [
    baseMixin({
      findData: rapidPartsPage,
      dataName: ['content', 'totalRecords'],
      idName: 'id',
    }),
  ],
  computed: {
    //优惠金额
    discountsAmount () {
      let sum = 0
      this.tableData.forEach((element) => {
        sum += Number(element.shouldAmount ? element.shouldAmount : 0)
      })
      return sum
    },
    jpSum () {
      let sum = 0
      this.tableData.forEach((element) => {
        sum += Number(element.amount)
      })
      return sum
    },
  },
  data () {
    return {
      searchOptions: {
        rapidId: this.workOrderId, //快速单id
        jpsType: 0 //可退
      },
      configOption: {},//相应接口配置
      tableColumnAttributes: [
        { name: '精品编号', value: 'orderNo', type: 'text' },
        { name: '精品名称', value: 'name', type: 'text' },
        { name: '一级类别', value: 'appLargeIdOneValue', type: 'text' },
        { name: '二级类别', value: 'appLargeIdTwoValue', type: 'text' },
        { name: '供应商', value: 'supplierName', type: 'text' },
        { name: '销售价', value: 'currentPrice', type: 'text' },
        { name: '数量', value: 'num', type: 'text' },
        { name: '折扣', value: 'discount', type: 'text' },
        { name: '折后总价', value: 'shouldAmount', type: 'render'},
        { name: '收费类型', value: 'tollType', type: 'text' },
        { name: '退料数量', value: 'returnNum', type: 'inputNumber' },
      ],
    }
  },
  async created () {
    this.configOption = this.getParentConfigOption(this.$parent, 'getConfig');
    console.log(this.configOption);
    this.notQuery = false;
    this.searchOptions[`${this.configOption.orderBoutiqueList.queryField}`] = this.workOrderId;
    this.setTableData()
  },
  methods: {
    //确认退料
    async confimReturn () {
      let jpList = this.tableData;
      let fun = this.configOption.orderBoutiqueList.save;
      if (fun) {
        let { success } = await fun({
          jpList,
          [this.configOption.orderBoutiqueList.queryField]: this.workOrderId
        });
        if (success) {
          this.$message.success('操作成功');
          this.$emit("dialogClose")
        }
      }
    },
    //获取父级配置
    getParentConfigOption (dom, actionName) {
      if (`${actionName}` in dom) {
        return dom[`${actionName}`];
      } else if ("$parent" in dom) {
        return this.getParentConfigOption(dom.$parent, actionName);
      } else {
        return;
      }
    },
    async setTableData () {
      if (!this.notQuery) {
        let fun = this.configOption.orderBoutiqueList.query;
        if (fun) {
          let res = await fun(this.searchOptions, this.pageSize, this.currentPage);
          this.totalCount = parseInt(res.data['totalRecords'])
          this.tableData = res.data['content'];
        }
      }
    },
    changeType (item) {
      this.selectedTypeText = item.label
    },
  },
  mounted () { },
}
</script>

<style lang="scss" scoped>
</style>
